package com.tim.wicket.examples;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;

import com.tim.wicket.examples.model.Cart;
import com.tim.wicket.examples.model.Cheese;

public class Index extends CheesrPage {

	private static final int CHEESES_PER_PAGE = 5;
	private static final long serialVersionUID = 1L;

	public Index() {
		add(new Link<Void>("toAnAjaxExample") {
			@Override
			public void onClick() {
				setResponsePage(AjaxExamplePage.class);
			}
		});
		
		PageableListView<Cheese> pageableCheeses = new PageableListView<Cheese>("cheeses", getCheeses(), CHEESES_PER_PAGE) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Cheese> item) {
				Cheese cheese = item.getModelObject();
				
				item.add(new Label("name", cheese.getName()));
				item.add(new Label("description", cheese.getDescription()));
				item.add(new Label("price", "$" + cheese.getPrice()));
				
				item.add(new Link<Cheese>("add", item.getModel()) {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick() {
						Cheese selected = getModelObject();
						getCart().getCheeses().add(selected);
					}
					
				});
				
			}
		};
		
		add(pageableCheeses);
		
		add(new PagingNavigator("cheeseNavigator", pageableCheeses));
		
		add(new ShoppingCartPanel("shoppingcart", getCart()));
		
		
		
		add(new Link<Cart>("checkout") {
			@Override
			public void onClick() {
				setResponsePage(CheckOut.class);
			}
			
			@Override
			public boolean isVisible() {
				return !getCart().getCheeses().isEmpty();
			}
		});
		
//		Options chartOptions = createChartOptions();
//		add(new Chart("chart", chartOptions));
	}

//	private Options createChartOptions() {
//		Options options = new Options();
//
//	    options
//	        .setChartOptions(new ChartOptions()
//	            .setType(SeriesType.LINE));
//
//	    options
//	        .setTitle(new Title("My very own chart."));
//
//	    options
//	        .setxAxis(new Axis()
//	            .setCategories(Arrays
//	                .asList(new String[] { "Jan", "Feb", "Mar", "Apr", "May",
//	                    "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" })));
//
//	    options
//	        .setyAxis(new Axis()
//	            .setTitle(new Title("Temperature (C)")));
//
//	    options
//	        .setLegend(new Legend()
//	            .setLayout(LegendLayout.VERTICAL)
//	            .setAlign(HorizontalAlignment.RIGHT)
//	            .setVerticalAlign(VerticalAlignment.TOP)
//	            .setX(-10)
//	            .setY(100)
//	            .setBorderWidth(0));
//
//	    options
//	        .addSeries(new SimpleSeries()
//	            .setName("Tokyo")
//	            .setData(
//	                Arrays
//	                    .asList(new Number[] { 7.0, 6.9, 9.5, 14.5, 18.2, 21.5,
//	                        25.2, 26.5, 23.3, 18.3, 13.9, 9.6 })));
//
//	    options
//	        .addSeries(new SimpleSeries()
//	            .setName("New York")
//	            .setData(
//	                Arrays
//	                    .asList(new Number[] { -0.2, 0.8, 5.7, 11.3, 17.0, 22.0,
//	                        24.8, 24.1, 20.1, 14.1, 8.6, 2.5 })));
//		
//	    return options;
//	}
	
}
